﻿using MS.Win32;
using System;
using System.Runtime.InteropServices;

namespace ThinkPadDockHelper
{
    internal static class PowerUtil
    {
        public static uint PowerGetActiveScheme(ref Guid activePolicyGuid)
        {
            uint rc = 0;

            var activePolicyGuidPtr = IntPtr.Zero;
            var size = Marshal.SizeOf(typeof(Guid));
            rc = NativeMethods.PowerGetActiveScheme(IntPtr.Zero, ref activePolicyGuidPtr);

            var tempBuffer = new byte[size];
            Marshal.Copy(activePolicyGuidPtr, tempBuffer, 0, size);
            activePolicyGuid = new Guid(tempBuffer);

            return rc;
        }

        public static uint PowerSetActiveScheme(ref Guid activePolicyGuid)
        {
            uint rc = 0;

            rc = NativeMethods.PowerSetActiveScheme(IntPtr.Zero, ref activePolicyGuid);

            return rc;
        }

        public static uint PowerReadACValueIndex(ref Guid schemeGuid, ref uint acValueIndex)
        {
            uint rc = 0;
            var subgroupGuid = NativeMethods.GUID_SYSTEM_BUTTON_SUBGROUP;
            var powerSettingGuid = NativeMethods.GUID_LIDCLOSE_ACTION;

            rc = NativeMethods.PowerReadACValueIndex(
                IntPtr.Zero,
                ref schemeGuid,
                ref subgroupGuid,
                ref powerSettingGuid,
                ref acValueIndex);

            return rc;
        }

        public static uint PowerWriteACValueIndex(ref Guid schemeGuid, uint acValueIndex)
        {
            uint rc = 0;
            var subgroupGuid = NativeMethods.GUID_SYSTEM_BUTTON_SUBGROUP;
            var powerSettingGuid = NativeMethods.GUID_LIDCLOSE_ACTION;

            rc = NativeMethods.PowerWriteACValueIndex(
                IntPtr.Zero,
                ref schemeGuid,
                ref subgroupGuid,
                ref powerSettingGuid,
                acValueIndex);

            return rc;
        }

        public static uint PowerReadDCValueIndex(ref Guid schemeGuid, ref uint dcValueIndex)
        {
            uint rc = 0;
            var subgroupGuid = NativeMethods.GUID_SYSTEM_BUTTON_SUBGROUP;
            var powerSettingGuid = NativeMethods.GUID_LIDCLOSE_ACTION;

            rc = NativeMethods.PowerReadDCValueIndex(
                IntPtr.Zero,
                ref schemeGuid,
                ref subgroupGuid,
                ref powerSettingGuid,
                ref dcValueIndex);

            return rc;
        }

        public static uint PowerWriteDCValueIndex(ref Guid schemeGuid, uint dcValueIndex)
        {
            uint rc = 0;
            var subgroupGuid = NativeMethods.GUID_SYSTEM_BUTTON_SUBGROUP;
            var powerSettingGuid = NativeMethods.GUID_LIDCLOSE_ACTION;

            rc = NativeMethods.PowerWriteDCValueIndex(
                IntPtr.Zero,
                ref schemeGuid,
                ref subgroupGuid,
                ref powerSettingGuid,
                dcValueIndex);

            return rc;
        }
    }
}
